<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
    </page-filter>
    <dialog-table
      ref="resourceTable"
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :row-key="rowKey"
      :checked-row-list="checkedRowList"
      :reserve-selection="true"
      :check-box="checkBox"
      :pager="pager"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-commonName="scope">
        {{ scope.row.commonName }}
      </template>
      <template v-slot:col-orderNo="scope">
        {{ scope.row.orderNo }}
      </template>
    </dialog-table>
  </page-dialog>
</template>
<script setup name="selectAsset">
import PageDialog from "../PageDialog/index";
import DialogTable from "../DialogTable/index";
import PageFilter from "../NewPageFilter/index";
import { getCustomizeSetting } from "@/api/customize";
import { getEquipmentArchivesCustomizePage } from "@/api/equipment";
const emits = defineEmits();
const { proxy } = getCurrentInstance();
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  rowKey: {
    type: String,
    default: "id",
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getEquipmentArchivesCustomizePage,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {};
    },
  },
  tableKey: {
    type: String,
    default:
      "assetDictDialog-com.slip.spd.entity.vo.asset.settings.AssetDictVO",
  },
  checkedList: {
    type: Array,
    default: () => {
      return [];
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return [];
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  pager: {
    type: Boolean,
    default: true,
  }
});

// 过滤相关配置
const filterInfo = ref({
  query: {
    page: true,
    customizeUserVO: {},
    sortString: proxy.sortString,
    queryMap: {
      // type: {
      //   asTbName: "core_product_new",
      //   columnName: "`type`",
      //   queryType: "EQUALS",
      //   queryCategory: "AND",
      //   value: "1",
      // },
      // status: {
      //   asTbName: "asset_purch_invoice_order",
      //   columnName: "`status`",
      //   queryType: "EQUALS",
      //   queryCategory: "AND",
      //   value: "4",
      // },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [{ type: "button", label: "搜索", event: search }],
});
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
});
// 弹窗相关
const dialogInfo = ref({
  title: "选择发票",
  width: "90%",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "保存",
      type: "primary",
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
});
const selectRow = ref([]);
const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emits("update:showDialog", newValue); // 更新父组件shows
  },
});

setData();
function setData() {
  let obj = {
    commonName: {
      label: "设备名称",
      value: "commonName",
      componentType: "input",
      show: false,
      type: "commonName",
      hidden: true,
    },
    statusValue: {
      label: "状态",
      value: "statusValue",
      asValue: "status",
      componentType: "select-model",
      code: "asset_purch_invoice_order_status",
      show: false,
      hidden: true,
      disabled: true
    },
  };
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  });
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  });
  getCustomize();
}

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random();
}
function search() {
  const _tableInfo = tableInfo.value;
  _tableInfo.initCurpage = Math.random();
  _tableInfo.refresh = Math.random();
}
function save() {
  handleClose();
  emits("callback", selectRow.value);
}
function handleEvent(event, data) {
  switch (event) {
    case "tableClickRow":
      selectRow.value = data;
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value;
      }
      break;
    case "tableCheck":
      selectRow.value = data;
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value;
      }
      break;
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false;
  emits("reset");
}
function getCustomize() {
  let _tableInfo = tableInfo.value;
  let _filterInfo = filterInfo.value;
  let param = { tableKey: props.tableKey };
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data;
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      );
      let searchInfo = proxy.$fn.setSearchList(
        _filterInfo.searchList,
        data.detail
      );
      _filterInfo.searchList = searchInfo.searchList;
      _filterInfo.searchList.forEach((item) => {
        item.event = search;
      });

      filterInfo.value.query.customizeUserVO = data;
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
        ...props.query,
      };
      filterInfo.value.query.queryParams = {
        ...filterInfo.value.query.queryParams,
        ...props.queryParams,
      };
      tableInfo.value.api = props.api;
      getList();
    }
  });
}
</script>

<style scoped lang="scss"></style>
